package com.ciei.dpagm.controller;

import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.controller.BaseController;
import com.ciei.dpagm.entity.Person;
import com.ciei.dpagm.service.PersonService;
import com.ciei.dpagm.util.DateUtil;
import com.ciei.dpagm.util.JsonUtil;
import com.ciei.dpagm.util.PageParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class PersonController extends BaseController {

    @Autowired
    private PersonService personService;

    @RequestMapping("person/personPage")
    public String personPage() {
        return "person/personPage";
    }
    @RequestMapping("person/personForm")
    public String personForm() {
        return "person/personForm";
    }


    @RequestMapping("person/add")
    @ResponseBody
    public JSONObject add(Person person) {
        JSONObject json = null;
        boolean bool = true;
        List<Person> list =personService.list();
        for (Person person1:list){
            if (person1.getMobileTel().equals(person.getMobileTel())){
                json=JsonUtil.getFail("该客户已存在");
                bool = false;
                break;
            }
        }
        if (bool){
            return personService.addPerson(person);
        }else {
            return  json;
        }
    }

    /**
     * 客户管理-新增联系人
     * 供应商管理-新增联系人
     */
    @RequiresPermissions(value = {"system:company:addPerson", "system:supplier:addPerson"}, logical = Logical.OR)
    @RequestMapping("vue/person/add")
    @ResponseBody
    public JSONObject addVue(@RequestBody JSONObject jsonObject) {
        return personService.addOrUpdatePerson(jsonObject,"add");
    }


    @RequestMapping("person/update")
    @ResponseBody
    public JSONObject update(Person person){
       return personService.updatePerson(person);
    }

    /**
     * 客户管理-修改联系人
     * 供应商管理-修改联系人
     */
    @RequiresPermissions(value = {"system:company:editPerson", "system:supplier:editPerson"}, logical = Logical.OR)
    @RequestMapping("vue/person/update")
    @ResponseBody
    public JSONObject updateVue(@RequestBody JSONObject jsonObject) {
        return personService.addOrUpdatePerson(jsonObject,"edit");
    }

    @RequestMapping("person/load")
    @ResponseBody
    public JSONObject loadPersonData(Integer companyId,String personName) {
        PageParam ps=getPageParams();
        Map<String, Object> params=ps.params;
        int posStart =ps.posStart;
        int count =ps.count;
        params.put("isDelete", 0);
        params.put("companyId", companyId);
//        params.put("createByName", createByName);
        JSONObject json = new JSONObject();

        json=personService.loadPersonData(posStart, count, params, new String[]{"personName"}, new String[]{"isDelete","companyId"});
        return json;
    }
    @RequestMapping("vue/person/load")
    @ResponseBody
    public JSONObject loadPersonDataVue(@RequestBody JSONObject page) {
        PageParam ps=getPageParams(page);

        return personService.loadPersonDataVue(ps);
    }

    @RequestMapping("/person/loadForm")
    @ResponseBody
    public JSONObject loadForm(String personId) {
        JSONObject json;
        JSONObject jsonData=new JSONObject();
        if (personId != null) {
            Integer person_Id=Integer.parseInt(personId);
            Person person=personService.findById(personId);
            if(person!=null){
                jsonData= (JSONObject)JSONObject.toJSON(person);
                jsonData.put("personName",person.getPersonName());
                jsonData.put("gender",person.getGender());
                jsonData.put("mobileTel",person.getMobileTel());
                jsonData.put("email",person.getEmail());
                jsonData.put("telephone",person.getTelephone());
                jsonData.put("userNote",person.getUserNote());
                if(person.getCreateTime()!=null){
                    jsonData.put("createTime",DateUtil.dateToString(person.getCreateTime(),DateUtil.FORMAT_LONG));
                }else{
                    jsonData.remove("createTime");
                }

            }
            json = JsonUtil.getSuccess("成功");
            json.put("data", jsonData);

        }else {
            json = JsonUtil.getFail("失败");
        }
        return json;
    }

    @RequestMapping("person/delete")
    public @ResponseBody JSONObject delete(String deleteIds) {
        JSONObject json;
        if (deleteIds!=null&&deleteIds!="")
        {
            String[] strs = deleteIds.split(",");
            for (String deleteId:strs)
            {
                Person person=personService.findById(Integer.valueOf(deleteId));
//				Region region=regionService.findById(Integer.valueOf(deleteId));

//               testProjectService.update(testProject);
//				regionService.removeById(region);
                personService.removeById(person);
//                faultReportService.updateById(person);
            }
//			PageParam pageParam=getPageParams();
//			Users user = (Users) findSessionKey(APP.SESSION_KEY_CURRENT_USER);

            json = JsonUtil.getSuccess("删除成功!");
            json.put("deleteIds",deleteIds);
            json.put("msg","删除成功!");

            return  json;

        }else
        {

            json = JsonUtil.getFail("删除失败!");
            json.put("msg","删除失败!");
            return  json;
        }
    }

    /**
     * 客户管理-删除联系人
     * 供应商管理-删除联系人
     */
    @RequiresPermissions(value = {"system:company:delPerson", "system:supplier:delPerson"}, logical = Logical.OR)
    @RequestMapping("vue/person/delete")
    @ResponseBody
    public JSONObject deleteVue(@RequestBody JSONObject jsonObject) {
        return personService.deleteVue(jsonObject);
    }


}
